package com.example.moneygrosService.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import com.example.moneygrosService.database.MoneyContract.BalancesColumns;
import com.example.moneygrosService.database.MoneyContract.PendingsColumns;
import com.example.moneygrosService.database.MoneyContract.UsersColumns;

public class MoneyDatabase extends SQLiteOpenHelper {
	
	private static final String TAG = "MoneyDatabase";

	private static final String DATABASE_NAME = "balance.db";
	private static final int VER_LAUNCH = 1;

	private static final int DATABASE_VERSION = VER_LAUNCH;
//	private SQLiteDatabase mDb;
	
	interface Tables {
		String BALANCES = "balances";
		String USERS = "users";
		String USER_PROFILES = "user_profiles";
		String PENDINGS = "pendings";
	}

	public MoneyDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
        Log.v("TATA", "MoneyDatabase creation" );

//        db.execSQL("CREATE TABLE " + Tables.BALANCES+ " ("
//		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
//		+ BalancesColumns.USER_ID + " INTEGER NOT NULL,"
//		+ BalancesColumns.BALANCE + " INTEGER,"
//		+ BalancesColumns.VERSION + " INTEGER NOT NULL)"); 
//        
//        mDb=db;
        db.execSQL("CREATE TABLE " + Tables.USERS+ " ("
        		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        		+ UsersColumns.USER_ID + " STRING UNIQUE,"        		
        		+ UsersColumns.BIDIRECTIONAL + " INTEGER,"        		
        		+ UsersColumns.BALANCE + " FLOAT,"
        		+ UsersColumns.USER_NAME+ " STRING NOT NULL,"
        		+ UsersColumns.PHONE_NUMBER+ " STRING,"
                + UsersColumns.BLUETOOTH_MAC+ " STRING,"
        		+ UsersColumns.VERSION + " INTEGER NOT NULL)");
        
        db.execSQL("CREATE TABLE " + Tables.USER_PROFILES+ " ("
        		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        		+ UsersColumns.USER_ID + " STRING UNIQUE,"
        		+ UsersColumns.USER_NAME+ " STRING NOT NULL,"
        		+ UsersColumns.PHONE_NUMBER+ " STRING,"
        		+ UsersColumns.BLUETOOTH_MAC+ " STRING,"
        		+ UsersColumns.VERSION + " INTEGER NOT NULL)");
//        db.execSQL("CREATE TABLE " + Tables.USERS+ " ("
//        		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
//        		+ UsersColumns.USER_ID + " STRING UNIQUE,"
//        		+ UsersColumns.USER_NAME+ " STRING NOT NULL,"
//        		+ UsersColumns.PHONE_NUMBER+ " STRING,"
//        		+ UsersColumns.VERSION + " INTEGER NOT NULL)");

        db.execSQL("CREATE TABLE " + Tables.BALANCES+ " ("
        		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        		+ BalancesColumns.USER_ID + " STRING UNIQUE,"
        		+ BalancesColumns.BALANCE + " FLOAT,"
        		+ BalancesColumns.VERSION + " INTEGER NOT NULL,"
        		+ "FOREIGN KEY ("+ BalancesColumns.USER_ID + ") REFERENCES " + Tables.USERS+ "("+UsersColumns.USER_ID+") ON DELETE CASCADE)");

        db.execSQL("CREATE TABLE " + Tables.PENDINGS+ " ("
        		+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        		+ PendingsColumns.OPERATION_ID + " STRING UNIQUE,"
        		+ PendingsColumns.USER_ID+ " STRING,"
        		+ PendingsColumns.OFFSET+ " FLOAT,"
        		+ PendingsColumns.OPERATION_STATUS+ " STRING NOT NULL,"
        		+ PendingsColumns.OPERATION_DATE+ " DATE NOT NULL,"
        		+ PendingsColumns.VERSION + " INTEGER NOT NULL,"
				+ "FOREIGN KEY ("+ PendingsColumns.USER_ID + ") REFERENCES " + Tables.USERS+ "("+UsersColumns.USER_ID+") ON DELETE CASCADE)");

        Log.v("TATA", "MoneyDatabase créé" );

	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

		Log.d(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);
//
//		// NOTE: This switch statement is designed to handle cascading database
//		// updates, starting at the current version and falling through to all
//		// future upgrade cases. Only use "break;" when you want to drop and
//		// recreate the entire database.
//		int version = oldVersion;
//
//		switch (version) {
//		case VER_LAUNCH:
//			//Here we handle the table changes...
//		}
//
//		Log.d(TAG, "After upgrade logic, at version " + version);
//		if (version != DATABASE_VERSION) {
//			Log.w(TAG, "Destroying old data during upgrade");
//			db.execSQL("DROP TABLE IF EXISTS " + Tables.EVENTS);
//			db.execSQL("DROP TABLE IF EXISTS " + Tables.TICKETS);
//			onCreate(db);
//		}
	}

}
